package com.xq.shop.controller;


import com.xq.shop.dto.EditBannerDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.IBannerService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.BannerVo;
import com.xq.shop.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 轮播表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-22
 */
@RestController
@RequestMapping("/banner")
@Slf4j
public class BannerController {

    private final IBannerService bannerService;
    private final IUserService userService;

    public BannerController(IBannerService bannerService, IUserService userService) {
        this.bannerService = bannerService;
        this.userService = userService;
    }

    @RequestMapping("/query")
    public BaseResult<List<BannerVo>> query(@RequestBody QueryDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<List<BannerVo>> result = new BaseResult<>();
        List<BannerVo> voList = bannerService.queryBanner(dto);
        result.initializeSuccess(voList);
        return result;
    }

    @RequestMapping("/edit")
    public BaseResult<Void> edit(@RequestBody @Validated EditBannerDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        bannerService.editBanner(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/remove")
    public BaseResult<Void> remove(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo vo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        bannerService.removeBanner(dto, vo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/test")
    public void remove() {
        log.info("定时任务开始执行");
        try {
            // 执行命令
            Runtime.getRuntime().exec("python C:\\getALiData\\GoodsNetworkByLeTianFixedTime.py");

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

}
